import { createRouter, createWebHistory, Router } from 'vue-router';
import routes from './router';
import { useUserStore } from '@/stores/user'

import { isWhiteListUser } from '@/utils';

const router: Router = createRouter({
    history: createWebHistory(),
    routes: routes
})

router.beforeEach((to) => {
    const userStore = useUserStore()
    const deviceIsUndefined = typeof to.query.device === 'undefined'
    const deviceIsEmpty = userStore.device.length === 0
    const notAndroid = typeof android === 'undefined'
    
    if (!isWhiteListUser(to.query.device as string || userStore.device) && notAndroid) {
        console.error('It is not a whitelisted user or Android')
        return false
    }

    if (deviceIsEmpty || !deviceIsUndefined) {
        userStore.$patch({
            device: to.query.device as string
        })
        userStore.handleGetUserInfo()
    }
})

export default router;